package com.trxy.controller.back;

import com.trxy.model.Product;
import com.trxy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/back/center/produ")
public class ProduController {
    @Autowired
    ProductService productService;

    //商品列表
    @RequestMapping("/list")
    String list(Model model) {
        List<Product> productList = productService.selectAll();
        model.addAttribute("ProductList", productList);
        return "back/produ/list";
    }

    @ResponseBody
    @RequestMapping("/add_do")
    String add_do(String title, String name, String price) {
        double price2 = Double.parseDouble(price);

        //存入数据库
        int num = productService.add(new Product(title, name, price2));
        if (num > 0) {
            //成功，重定向到列表页
            return "ok";
        } else {
            //失败
            return "fail";
        }
    }

    @ResponseBody
    @RequestMapping("/edit")
    Product edit(int id) {
        Product product = productService.selectByPrimarykey(id);
        return product;
    }


    @ResponseBody
    @RequestMapping("/edit_do")
    String edit_do(Product product) {
        int num = productService.edit(product);
        if (num > 0) return "ok";
        else return "fail";
    }

    @ResponseBody
    @RequestMapping("/del")
    String del(int id) {
        int num = productService.del(id);
        if (num > 0) return "ok";
        else return "fail";
    }
}
